﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNAGameEngine;
using XNAGameEngine.GameState;
using XNAGameEngine.GUI;
using XNAGameEngine.Input;
using XNAGameEngine.GameObject;
using XNAGameEngine.Audio;

namespace XNAGame 
{
    class Demostate : GameState
    {
        #region Private Vars
        int _testWarrior1ID, _testWarrior2ID, _testWarrior3ID, _testWarrior4ID;     
                //Hard coded ints to hold player controlled chracter IDs
        int _testWarrior5ID, _testWarrior6ID, _testWarrior7ID, _testWarrior8ID, _testWarrior9ID, _testWarrior10ID;
                //Hard coded ints to hold AI controlled character IDs
        int _MenuID;
                //Int to hold the menu object's ID
        Texture2D background;

        Warrior testWarrior1;
        Warrior testWarrior2;
        Warrior testWarrior3;
        Warrior testWarrior4;
        Warrior testWarrior5;
        Warrior testWarrior6;
        Warrior testWarrior7;
        Warrior testWarrior8;
        Warrior testWarrior9;
        Warrior testWarrior10;
                //Ten hard coded character(warrior) objects
        map BattleMap;
                //map object
        battlemenu menu;
                //battlemenu object
        Warrior[] dudes;
                //An array of warriors to hold Warriors created in the battle state
        int[] dudeIDs;
                //An array of ints to hold the chracter's object IDs
        int numberOfdudes;
                //An int to hold the total number of Warriors created
        int whoseturn;
                //An int to keep track of the turns
        #endregion 

        #region Constructor
        public Demostate(string stringReference, IManagerGameState stateManager)
            : base(stringReference, stateManager)
        { 
            numberOfdudes = 0;
        }
        #endregion

        #region Initialize
        public override void Initialize()
        {
            GameEngine.GetService<IManagerGUI>().LoadGUILayout("ForceGameOverGUI");
            background = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\battleBG");
            //GameEngine.GetService<IManagerAudio2D>().NextSong();

            BattleMap = new map();          //create a new map
            dudes = new Warrior[16];        //create a new Warrior array (max 16 warriors)
            dudeIDs = new int[16];          //create a new ID array (again max 16 IDs)
            whoseturn = 0;                  //set initial turns to 0
            numberOfdudes = 0;              //set initial number of warriors created to 0

            skill attack = new skill();     //create a new skill => attack
            skill burst = new skill();      //create a new skill => burst
            burst.Name = "Burst";           //Set burst name field => "Burst"
            burst.Range = 3;                //Set burst range field => 3
            burst.Splash = false;           //set burst splash field => false
            burst.Power = 70;               //set burst power field => 70

            #region Hard Coded Characters
            _testWarrior1ID = GameEngine.GetService<IManagerGameObject2D>().CreateGameObject("Warrior");
            testWarrior1 = (Warrior)GameEngine.GetService<IManagerGameObject2D>().GetGameObjectById(_testWarrior1ID);
            testWarrior1.IsAIcontrolled = true;
            testWarrior1.SetTexture(2);
            testWarrior1.Facing = "North";
            testWarrior1.Name = "goodie5";
            testWarrior1.team = 0;
            testWarrior1.Map_x = 1;
            testWarrior1.Map_y = 7;
            testWarrior1.skills[0] = attack;
            testWarrior1.skills[1] = burst;
            dudes[numberOfdudes] = testWarrior1;
            dudeIDs[numberOfdudes] = _testWarrior1ID;

            testWarrior1.MovePosition(BattleMap.Grid[testWarrior1.Map_x, testWarrior1.Map_y].Screenx,
                BattleMap.Grid[testWarrior1.Map_x, testWarrior1.Map_y].Screeny);
            BattleMap.Grid[testWarrior1.Map_x, testWarrior1.Map_y].Occupant = testWarrior1.Name;
            numberOfdudes++;

            _testWarrior2ID = GameEngine.GetService<IManagerGameObject2D>().CreateGameObject("Warrior");
            testWarrior2 = (Warrior)GameEngine.GetService<IManagerGameObject2D>().GetGameObjectById(_testWarrior2ID);
            testWarrior2.IsAIcontrolled = true;
            testWarrior2.SetTexture(2);
            testWarrior2.Facing = "North";
            testWarrior2.Name = "goodie4";
            testWarrior2.team = 0;
            testWarrior2.Map_x = 4;
            testWarrior2.Map_y = 7;
            testWarrior2.skills[0] = attack;
            testWarrior2.skills[1] = burst;
            dudes[numberOfdudes] = testWarrior2;
            dudeIDs[numberOfdudes] = _testWarrior2ID;

            testWarrior2.MovePosition(BattleMap.Grid[testWarrior2.Map_x, testWarrior2.Map_y].Screenx,
                BattleMap.Grid[testWarrior2.Map_x, testWarrior2.Map_y].Screeny);
            BattleMap.Grid[testWarrior2.Map_x, testWarrior2.Map_y].Occupant = testWarrior2.Name;
            numberOfdudes++;

            _testWarrior3ID = GameEngine.GetService<IManagerGameObject2D>().CreateGameObject("Warrior");
            testWarrior3 = (Warrior)GameEngine.GetService<IManagerGameObject2D>().GetGameObjectById(_testWarrior3ID);
            testWarrior3.IsAIcontrolled = true;
            testWarrior3.SetTexture(2);
            testWarrior3.Facing = "North";
            testWarrior3.Name = "goodie3";
            testWarrior3.team = 0;
            testWarrior3.Map_x = 3;
            testWarrior3.Map_y = 8;
            testWarrior3.skills[0] = attack;
            testWarrior3.skills[1] = burst;
            dudes[numberOfdudes] = testWarrior3;
            dudeIDs[numberOfdudes] = _testWarrior3ID;

            testWarrior3.MovePosition(BattleMap.Grid[testWarrior3.Map_x, testWarrior3.Map_y].Screenx,
                BattleMap.Grid[testWarrior3.Map_x, testWarrior3.Map_y].Screeny);
            BattleMap.Grid[testWarrior3.Map_x, testWarrior3.Map_y].Occupant = testWarrior3.Name;
            numberOfdudes++;

            _testWarrior4ID = GameEngine.GetService<IManagerGameObject2D>().CreateGameObject("Warrior");
            testWarrior4 = (Warrior)GameEngine.GetService<IManagerGameObject2D>().GetGameObjectById(_testWarrior4ID);
            testWarrior4.IsAIcontrolled = true;
            testWarrior4.SetTexture(2);
            testWarrior4.Facing = "North";
            testWarrior4.Name = "goodie2";
            testWarrior4.team = 0;
            testWarrior4.Map_x = 6;
            testWarrior4.Map_y = 8;
            testWarrior4.skills[0] = attack;
            testWarrior4.skills[1] = burst;
            dudes[numberOfdudes] = testWarrior4;
            dudeIDs[numberOfdudes] = _testWarrior4ID;

            testWarrior4.MovePosition(BattleMap.Grid[testWarrior4.Map_x, testWarrior4.Map_y].Screenx,
                BattleMap.Grid[testWarrior4.Map_x, testWarrior4.Map_y].Screeny);
            BattleMap.Grid[testWarrior4.Map_x, testWarrior4.Map_y].Occupant = testWarrior4.Name;
            numberOfdudes++;

            _testWarrior5ID = GameEngine.GetService<IManagerGameObject2D>().CreateGameObject("Warrior");
            testWarrior5 = (Warrior)GameEngine.GetService<IManagerGameObject2D>().GetGameObjectById(_testWarrior5ID);
            testWarrior5.IsAIcontrolled = true;
            testWarrior5.SetTexture(2);
            testWarrior5.Facing = "North";
            testWarrior5.Name = "goodie";
            testWarrior5.team = 0;
            testWarrior5.Map_x = 8;
            testWarrior5.Map_y = 7;
            testWarrior5.skills[0] = attack;
            testWarrior5.skills[1] = burst;
            dudes[numberOfdudes] = testWarrior5;
            dudeIDs[numberOfdudes] = _testWarrior5ID;

            testWarrior5.MovePosition(BattleMap.Grid[testWarrior5.Map_x, testWarrior5.Map_y].Screenx,
                BattleMap.Grid[testWarrior5.Map_x, testWarrior5.Map_y].Screeny);
            BattleMap.Grid[testWarrior5.Map_x, testWarrior5.Map_y].Occupant = testWarrior5.Name;
            numberOfdudes++;

            _testWarrior6ID = GameEngine.GetService<IManagerGameObject2D>().CreateGameObject("Warrior");
            testWarrior6 = (Warrior)GameEngine.GetService<IManagerGameObject2D>().GetGameObjectById(_testWarrior6ID);
            testWarrior6.IsAIcontrolled = true;
            testWarrior6.SetTexture(5);
            testWarrior6.Facing = "South";
            testWarrior6.Name = "Baddie2";
            testWarrior6.team = 1;
            testWarrior6.Map_x = 2;
            testWarrior6.Map_y = 3;
            testWarrior6.skills[0] = attack;
            testWarrior6.skills[1] = burst;
            dudes[numberOfdudes] = testWarrior6;
            dudeIDs[numberOfdudes] = _testWarrior6ID;

            testWarrior6.MovePosition(BattleMap.Grid[testWarrior6.Map_x, testWarrior6.Map_y].Screenx,
                BattleMap.Grid[testWarrior6.Map_x, testWarrior6.Map_y].Screeny);
            BattleMap.Grid[testWarrior6.Map_x, testWarrior6.Map_y].Occupant = testWarrior6.Name;
            numberOfdudes++;

            _testWarrior7ID = GameEngine.GetService<IManagerGameObject2D>().CreateGameObject("Warrior");
            testWarrior7 = (Warrior)GameEngine.GetService<IManagerGameObject2D>().GetGameObjectById(_testWarrior7ID);
            testWarrior7.IsAIcontrolled = true;
            testWarrior7.SetTexture(5);
            testWarrior7.Facing = "South";
            testWarrior7.Name = "Baddie3";
            testWarrior7.team = 1;
            testWarrior7.Map_x = 4;
            testWarrior7.Map_y = 3;
            testWarrior7.skills[0] = attack;
            testWarrior7.skills[1] = burst;
            dudes[numberOfdudes] = testWarrior7;
            dudeIDs[numberOfdudes] = _testWarrior7ID;

            testWarrior7.MovePosition(BattleMap.Grid[testWarrior7.Map_x, testWarrior7.Map_y].Screenx,
                BattleMap.Grid[testWarrior7.Map_x, testWarrior7.Map_y].Screeny);
            BattleMap.Grid[testWarrior7.Map_x, testWarrior7.Map_y].Occupant = testWarrior7.Name;
            numberOfdudes++;

            testWarrior6.MovePosition(BattleMap.Grid[testWarrior6.Map_x, testWarrior6.Map_y].Screenx,
                BattleMap.Grid[testWarrior6.Map_x, testWarrior6.Map_y].Screeny);
            BattleMap.Grid[testWarrior6.Map_x, testWarrior6.Map_y].Occupant = testWarrior6.Name;
            numberOfdudes++;

            _testWarrior8ID = GameEngine.GetService<IManagerGameObject2D>().CreateGameObject("Warrior");
            testWarrior8 = (Warrior)GameEngine.GetService<IManagerGameObject2D>().GetGameObjectById(_testWarrior8ID);
            testWarrior8.IsAIcontrolled = true;
            testWarrior8.SetTexture(5);
            testWarrior8.Facing = "South";
            testWarrior8.Name = "Baddie4";
            testWarrior8.team = 1;
            testWarrior8.Map_x = 3;
            testWarrior8.Map_y = 2;
            testWarrior8.skills[0] = attack;
            testWarrior8.skills[1] = burst;
            dudes[numberOfdudes] = testWarrior8;
            dudeIDs[numberOfdudes] = _testWarrior8ID;

            testWarrior8.MovePosition(BattleMap.Grid[testWarrior8.Map_x, testWarrior8.Map_y].Screenx,
                BattleMap.Grid[testWarrior8.Map_x, testWarrior8.Map_y].Screeny);
            BattleMap.Grid[testWarrior8.Map_x, testWarrior8.Map_y].Occupant = testWarrior8.Name;
            numberOfdudes++;

            _testWarrior9ID = GameEngine.GetService<IManagerGameObject2D>().CreateGameObject("Warrior");
            testWarrior9 = (Warrior)GameEngine.GetService<IManagerGameObject2D>().GetGameObjectById(_testWarrior9ID);
            testWarrior9.IsAIcontrolled = true;
            testWarrior9.SetTexture(5);
            testWarrior9.Facing = "South";
            testWarrior9.Name = "Baddie5";
            testWarrior9.team = 1;
            testWarrior9.Map_x = 6;
            testWarrior9.Map_y = 3;
            testWarrior9.skills[0] = attack;
            testWarrior9.skills[1] = burst;
            dudes[numberOfdudes] = testWarrior9;
            dudeIDs[numberOfdudes] = _testWarrior9ID;

            testWarrior9.MovePosition(BattleMap.Grid[testWarrior9.Map_x, testWarrior9.Map_y].Screenx,
                BattleMap.Grid[testWarrior9.Map_x, testWarrior9.Map_y].Screeny);
            BattleMap.Grid[testWarrior9.Map_x, testWarrior9.Map_y].Occupant = testWarrior9.Name;
            numberOfdudes++;

            _testWarrior10ID = GameEngine.GetService<IManagerGameObject2D>().CreateGameObject("Warrior");
            testWarrior10 = (Warrior)GameEngine.GetService<IManagerGameObject2D>().GetGameObjectById(_testWarrior10ID);
            testWarrior10.IsAIcontrolled = true;
            testWarrior10.SetTexture(5);
            testWarrior10.Facing = "South";
            testWarrior10.Name = "Baddie6";
            testWarrior10.team = 1;
            testWarrior10.Map_x = 7;
            testWarrior10.Map_y = 2;
            testWarrior10.skills[0] = attack;
            testWarrior10.skills[1] = burst;
            dudes[numberOfdudes] = testWarrior9;
            dudeIDs[numberOfdudes] = _testWarrior9ID;

            testWarrior10.MovePosition(BattleMap.Grid[testWarrior10.Map_x, testWarrior10.Map_y].Screenx,
                BattleMap.Grid[testWarrior10.Map_x, testWarrior10.Map_y].Screeny);
            BattleMap.Grid[testWarrior10.Map_x, testWarrior10.Map_y].Occupant = testWarrior10.Name;
            numberOfdudes++;
            #endregion

            #region Initialize Menu
            _MenuID = GameEngine.GetService<IManagerGameObject2D>().CreateGameObject("BattleMenu");
            menu = (battlemenu)GameEngine.GetService<IManagerGameObject2D>().GetGameObjectById(_MenuID);

            GameEngine.GetService<IManagerGUI>().LoadGUILayout("DemoGUI");
                //Create the battle menu object
            menu.LoadMap(BattleMap);        //Pass it the map BattleMap
            menu.LoadParties(dudes);        //Pass it the array of warriors dudes
            menu.Currdude = testWarrior1;   //Set the initial currentdude => testWarrior1
            menu.Initialize();              //Initialize the rest of the menu fields
            #endregion

            base.Initialize();

        }
        #endregion

        #region Update
        public override void Update(GameTime gameTime)
        {           
            if (menu.hasendturn == 1)
                //If the current chracter's turn has ended
            {
                if (whoseturn == (numberOfdudes - 1))
                    //Check to see if all the character have taken a turn this round
                {
                    whoseturn = 0;          //If they have reset the turns
                    Console.WriteLine("whoseturn = 0");
                    menu.Initialize();      //Re-initialize the menu to reset the fields
                }
                else 
                    //If there are chracters who have not moved this round
                {
                    whoseturn++;            //Increment whoseturn and go to the next chracter
                    Console.WriteLine("whoseturn = " + whoseturn);
                    menu.Initialize();      //Start the next chracters turn
                }       
            }

            //Passing the battle menu a warrior who takes their turn. 
            if (whoseturn == 0)             //Had to hard code this and manually set all the fields
            {
                menu.Currdude = testWarrior1;   //Put this Warrior into the battle menu as currentdude
                testWarrior1.IsMoving = true;   //Make sure this is the only chracter moving onscreen
                testWarrior2.IsMoving = false;
                testWarrior3.IsMoving = false;
                testWarrior4.IsMoving = false;
                testWarrior5.IsMoving = false;
                testWarrior6.IsMoving = false;
                testWarrior7.IsMoving = false;
                testWarrior8.IsMoving = false;
                testWarrior9.IsMoving = false;
                testWarrior10.IsMoving = false;

            }
            else if (whoseturn == 1)
            {
                menu.Currdude = testWarrior6;   //Put this Warrior into the battle menu as currentdude
                testWarrior1.IsMoving = false;  //Make sure this is the only chracter moving onscreen
                testWarrior2.IsMoving = false;
                testWarrior3.IsMoving = false;
                testWarrior4.IsMoving = false;
                testWarrior5.IsMoving = false;
                testWarrior6.IsMoving = true;
                testWarrior7.IsMoving = false;
                testWarrior8.IsMoving = false;
                testWarrior9.IsMoving = false;
                testWarrior10.IsMoving = false;
            }
            else if (whoseturn == 2)
            {
                menu.Currdude = testWarrior2;   //Put this Warrior into the battle menu as currentdude
                testWarrior1.IsMoving = false;  //Make sure this is the only chracter moving onscreen
                testWarrior2.IsMoving = true;
                testWarrior3.IsMoving = false;
                testWarrior4.IsMoving = false;
                testWarrior5.IsMoving = false;
                testWarrior6.IsMoving = false;
                testWarrior7.IsMoving = false;
                testWarrior8.IsMoving = false;
                testWarrior9.IsMoving = false;
                testWarrior10.IsMoving = false;
            }
            else if (whoseturn == 3)
            {
                menu.Currdude = testWarrior7;
                testWarrior1.IsMoving = false;  //Make sure this is the only chracter moving onscreen
                testWarrior2.IsMoving = false;  //Put this Warrior into the battle menu as currentdude
                testWarrior3.IsMoving = false;
                testWarrior4.IsMoving = false;
                testWarrior5.IsMoving = false;
                testWarrior6.IsMoving = false;
                testWarrior7.IsMoving = true;
                testWarrior8.IsMoving = false;
                testWarrior9.IsMoving = false;
                testWarrior10.IsMoving = false;
            }
            else if (whoseturn == 4)
            {
                menu.Currdude = testWarrior3;
                testWarrior1.IsMoving = false;  //Make sure this is the only chracter moving onscreen
                testWarrior2.IsMoving = false;
                testWarrior3.IsMoving = true;
                testWarrior4.IsMoving = false;
                testWarrior5.IsMoving = false;
                testWarrior6.IsMoving = false;
                testWarrior7.IsMoving = false;
                testWarrior8.IsMoving = false;
                testWarrior9.IsMoving = false;
                testWarrior10.IsMoving = false;
            }
            else if (whoseturn == 5)
            {
                menu.Currdude = testWarrior8;   //Put this Warrior into the battle menu as currentdude
                testWarrior1.IsMoving = false;  //Make sure this is the only chracter moving onscreen
                testWarrior2.IsMoving = false;
                testWarrior3.IsMoving = false;
                testWarrior4.IsMoving = false;
                testWarrior5.IsMoving = false;
                testWarrior6.IsMoving = false;
                testWarrior7.IsMoving = false;
                testWarrior8.IsMoving = true;
                testWarrior9.IsMoving = false;
                testWarrior10.IsMoving = false;
            }
            else if (whoseturn == 6)
            {
                menu.Currdude = testWarrior4;   //Put this Warrior into the battle menu as currentdude
                testWarrior1.IsMoving = false;  //Make sure this is the only chracter moving onscreen
                testWarrior2.IsMoving = false;
                testWarrior3.IsMoving = false;
                testWarrior4.IsMoving = true;
                testWarrior5.IsMoving = false;
                testWarrior6.IsMoving = false;
                testWarrior7.IsMoving = false;
                testWarrior8.IsMoving = false;
                testWarrior9.IsMoving = false;
                testWarrior10.IsMoving = false;
            }
            else if (whoseturn == 7)
            {
                menu.Currdude = testWarrior9;   //Put this Warrior into the battle menu as currentdude
                testWarrior1.IsMoving = false;  //Make sure this is the only chracter moving onscreen
                testWarrior2.IsMoving = false;
                testWarrior3.IsMoving = false;
                testWarrior4.IsMoving = false;
                testWarrior5.IsMoving = false;
                testWarrior6.IsMoving = false;
                testWarrior7.IsMoving = false;
                testWarrior8.IsMoving = false;
                testWarrior9.IsMoving = true;
                testWarrior10.IsMoving = false;
            }
            else if (whoseturn == 8)
            {
                menu.Currdude = testWarrior5;   //Put this Warrior into the battle menu as currentdude
                testWarrior1.IsMoving = false;  //Make sure this is the only chracter moving onscreen
                testWarrior2.IsMoving = false;
                testWarrior3.IsMoving = false;
                testWarrior4.IsMoving = false;
                testWarrior5.IsMoving = true;
                testWarrior6.IsMoving = false;
                testWarrior7.IsMoving = false;
                testWarrior8.IsMoving = false;
                testWarrior9.IsMoving = false;
                testWarrior10.IsMoving = false;
            }
            else if (whoseturn == 9)
            {
                menu.Currdude = testWarrior10;  //Put this Warrior into the battle menu as currentdude
                testWarrior1.IsMoving = false;  //Make sure this is the only chracter moving onscreen
                testWarrior2.IsMoving = false;
                testWarrior3.IsMoving = false;
                testWarrior4.IsMoving = false;
                testWarrior5.IsMoving = false;
                testWarrior6.IsMoving = false;
                testWarrior7.IsMoving = false;
                testWarrior8.IsMoving = false;
                testWarrior9.IsMoving = false;
                testWarrior10.IsMoving = true;
            }

            base.Update(gameTime);
        }
        #endregion

        #region Draw
        public override void Draw(GameTime gameTime)
        {
           
            base.Draw(gameTime);

            Game.GraphicsDevice.Clear(Color.Black);

            GameEngine.SpriteBatch.Begin();

            GameEngine.SpriteBatch.Draw(
                 background,
                 new Rectangle(0, 0, background.Width,
                 background.Height), null,
                 Color.White, 0, Vector2.Zero,
                 SpriteEffects.None, 0);

            GameEngine.SpriteBatch.End();
        }
        #endregion

        #region Clean up
        public override void Cleanup()
        {
            testWarrior1.Destroy();
            testWarrior2.Destroy();
            testWarrior3.Destroy();
            testWarrior4.Destroy();
            testWarrior5.Destroy();
            testWarrior6.Destroy();
            testWarrior7.Destroy();
            testWarrior8.Destroy();
            testWarrior9.Destroy();
            testWarrior10.Destroy();
            BattleMap.DestroyMap();
            menu.Destroy();
            base.Cleanup();
        }
        #endregion
    }
}
